package com.cloud.school.bus.teacherhelper.modules.home.adapter;

import java.io.File;
import java.util.List;

import com.android.support.jhf.universalimageloader.core.ImageLoader;
import com.android.support.jhf.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.android.support.jhf.utils.ImageUtil;
import com.android.support.jhf.utils.ScreenUtils;
import com.android.support.jhf.utils.ToolUtils;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.TeacherHelperApplication;
import com.cloud.school.bus.teacherhelper.entitys.Picture;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;

public class PictureThumbnailsAdapter extends PagerAdapter {

	private Context mContext;
	private TeacherHelperApplication mApplication;
	private OnClickListener mOnClickListener;

	private List<Picture> mPictures;

	public PictureThumbnailsAdapter(Context context, List<Picture> pictures) {
		mContext = context;
		mPictures = pictures;
		mApplication = (TeacherHelperApplication) mContext
				.getApplicationContext();
	}

	@Override
	public Object instantiateItem(View container, final int position) {
		View view = LayoutInflater.from(mContext).inflate(
				R.layout.adapter_picture_thumbnails, null);

		Picture picture = mPictures.get(position);

		ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
		ImageLoader.getInstance().displayImage(picture.getPicturePath(),
				imageView, mApplication.mNoCacheDisplayImageOptions,
				new SimpleImageLoadingListener() {
					@Override
					public void onLoadingComplete(String imageUri, View view,
							Bitmap loadedImage) {
						ImageView tempImageView = (ImageView) view;
//						tempImageView.setImageBitmap(loadedImage);
						if(imageUri.startsWith("http://")){
							tempImageView.setImageBitmap(loadedImage);
						}else if(imageUri.startsWith("file:///")){
							ImageUtil.setRotaingImageBitmap(imageUri.replace("file:///", "/"), loadedImage, tempImageView);
						}else {
							ImageUtil.setRotaingImageBitmap(imageUri, loadedImage, tempImageView);
						}
					}
				});
		imageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (null != mOnClickListener) {
					mOnClickListener.onClick(v);
				}
			}
		});
		((ViewPager) container).addView(view);

		return view;
	}

	public void setOnClickListener(OnClickListener onClickListener) {
		mOnClickListener = onClickListener;
	}

	@Override
	public void destroyItem(View container, int position, Object object) {
		((ViewPager) container).removeView((View) object);
	}

	@Override
	public void startUpdate(View container) {
		// Log.d(TAG, "startUpdate");
	}

	@Override
	public void finishUpdate(View container) {
		// Log.d(TAG, "finishUpdate");
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		// Log.d(TAG, "isViewFromObject");
		return view == object;
	}

	@Override
	public Parcelable saveState() {
		// Log.d(TAG, "saveState");
		return null;
	}

	@Override
	public void restoreState(Parcelable state, ClassLoader loader) {
		// Log.d(TAG, "restoreState");
	}

	@Override
	public int getCount() {
		return mPictures.size();
	}

	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

}
